package eddy.yang.wordpad;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;

public class MyDropTargetListener implements DropTargetListener {
	WordPadFrame wordpadfrm;
	private String filename = null;
	String titleString = "";
	public MyDropTargetListener(WordPadFrame wordpadfrm) {
		this.wordpadfrm = wordpadfrm;
	}
	
	public void dragEnter(DropTargetDragEvent event) {
	}

	public void dragExit(DropTargetEvent event) {
	}            
	
	public void dragOver(DropTargetDragEvent event) {
	}

	public void drop(DropTargetDropEvent event) {
		event.acceptDrop(DnDConstants.ACTION_COPY);
		
		Transferable transferable = event.getTransferable();
		DataFlavor[] flavors = transferable.getTransferDataFlavors();
		for(int i = 0; i < flavors.length; i++) {
			DataFlavor d = flavors[i];
			
			try {
				if(d.equals(DataFlavor.javaFileListFlavor)) {
					java.util.List<File> fileList = (java.util.List<File>) transferable.getTransferData(d);
					for(File f : fileList) {
						titleString = f.getName();
						filename = f.getPath();
						openFile();
					}
				}
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public void dropActionChanged(DropTargetDragEvent event) {
	}
	
	public void openFile() {
		WordPadPanel pane = new WordPadPanel(wordpadfrm);
		if(!pane.doOpenCommand(filename)) return;
		wordpadfrm.wordpaneVect.add(pane);
		wordpadfrm.updateUI();
	}
}
